package com.gly091020.util;

import com.gly091020.BilibiliMedia;
import com.google.common.base.Objects;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class BilibiliMediaUtil {
    private static Map<String, String> DATA;
    public static Path getDownloadPath(){
        var p = FabricLoader.getInstance().getGameDir().resolve("BiliBiliMediaFiles");
        if (!p.toFile().isDirectory() && !p.toFile().mkdir()) {
            throw new RuntimeException("文件夹创建失败");
        }
        return p;
    }

    public static URI getUri(File file){
        return URI.create("http://127.0.0.1:" + SimpleFileServer.PORT + "/download?file=" + file.getName());
    }

    public static void loadJson(){
        var p = FabricLoader.getInstance().getGameDir().resolve("BiliBiliMediaFiles").resolve("video.json");
        if(!p.toFile().isFile()){
            try {
                if(!p.toFile().createNewFile()){
                    throw new IOException("文件创建失败");
                }
                DATA = new HashMap<>();
                saveJson();
                return;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        try {
            DATA = gson.fromJson(new String(Files.readAllBytes(p)), new TypeToken<Map<String, String>>() {}.getType());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void saveJson(){
        var p = FabricLoader.getInstance().getGameDir().resolve("BiliBiliMediaFiles").resolve("video.json");
        if(!p.toFile().isFile()){
            try {
                if(!p.toFile().createNewFile()){
                    throw new IOException("文件创建失败");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        try {
            Files.write(p, gson.toJson(DATA).getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Nullable
    public static String tryGetLocalFile(String url){
        return DATA.get(url);
    }

    public static void updateVideoFile(String url, File file){
        DATA.put(url, getUri(file).toString());
        saveJson();
    }

    public static void clearFile(){
        var p = FabricLoader.getInstance().getGameDir().resolve("BiliBiliMediaFiles");
        try {
            Files.delete(p);
        } catch (IOException ignored) {
        }
    }

    public static boolean isGLY(){
        return Objects.equal(BilibiliMedia._5112151111121, MinecraftClient.getInstance().getSession().getUuidOrNull());
    }
}
